package simulation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

/**
 * Authors: Kyle Martin, Ben Lee
 * Purpose: 
 * Version 3
 * Last updated: 11/22/11
 * Tested by:
 * Tested on:
 */
public class SimulationDisplay extends JFrame{
	
	Simulation sim;
	ChangeScheduleDialog changeSchedule;
	ScoreDisplay score;
	
	private JButton btnStart;
	private JButton btnStop;
	private JButton btnViewSchedule;
	private JButton btnScore;
	private JButton btnSkip;
//	private JTextArea schedule;
	private JPanel buttonPanel;
	
	private JPanel runwayPanel;
	private JLabel[] runway;
	private JLabel[] runwayStatus;
	private ImageIcon openIcon = new ImageIcon("img/OpenIcon.png");
	private ImageIcon closedIcon = new ImageIcon("img/ClosedIcon.png");
	private ImageIcon takeoffIcon = new ImageIcon("img/TakeoffIcon.png");
	private ImageIcon landingIcon = new ImageIcon("img/LandingIcon.png");
	
	private ImageIcon sunIcon = new ImageIcon("img/sunIcon.png");
	private ImageIcon rainIcon = new ImageIcon("img/rainIcon.png");
	private ImageIcon windIcon = new ImageIcon("img/windIcon.png");
	private ImageIcon snowIcon = new ImageIcon("img/snowIcon.png");
	
	private JLabel weatherLabel;
	private JPanel queuePanel;
	private JTextArea takeoff;
	private JTextArea landing;
	
	private final static String newline = "\n";
	
	
	public SimulationDisplay(Simulation s){
		
		btnStart = new JButton("Start");
		btnStart.setEnabled(false);
		btnStop = new JButton("Stop");
		btnViewSchedule  = new JButton("Edit Schedule");
		btnScore = new JButton("Score");
		btnSkip = new JButton("Skip Ahead");
//		schedule = new JTextArea("Schedule goes here.");
		buttonPanel = new JPanel();
		runwayPanel = new JPanel();
		
		queuePanel = new JPanel();
		takeoff = new JTextArea("Takeoff queue");
		landing = new JTextArea("Landing queue");
		
		runway = new JLabel[4];
		runwayStatus = new JLabel[4];
		for(int i = 0; i < 4; i++){
			runway[i] = new JLabel("Runway " + Integer.toString(i));
			runway[i].setHorizontalAlignment(SwingConstants.CENTER);
			runwayStatus[i] = new JLabel();
		}
		
		weatherLabel = new JLabel();
		weatherLabel.setBorder(new LineBorder(Color.BLACK));
		updateWeather(WeatherType.SUN);
		
		sim = s;
		score = new ScoreDisplay(sim);
		score.setVisible(false);
		changeSchedule = new ChangeScheduleDialog(sim);
		changeSchedule.setVisible(true);
		this.setLayout(new BorderLayout());
		
		btnStart.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				sim.start();
				btnStart.setEnabled(false);
				btnStop.setEnabled(true);
			}
		});
		btnStop.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				sim.stop();
				btnStop.setEnabled(false);
				btnStart.setEnabled(true);
			}
		});
		btnViewSchedule.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				changeSchedule.setVisible(true);
			}
		});
		btnScore.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				score.setVisible(true);
			}
		});
		btnSkip.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				sim.skip();
				if(sim.eventPriorityQueue.isEmpty()){
					btnSkip.setEnabled(false);
					repaint();
				}
			}
		});
		
		buttonPanel.setLayout(new GridLayout(5,1));
		buttonPanel.add(btnStart);
		buttonPanel.add(btnStop);
		buttonPanel.add(btnViewSchedule);
		buttonPanel.add(btnSkip);
		buttonPanel.add(btnScore);
		buttonPanel.setBorder(new LineBorder(Color.BLACK));
		this.add(buttonPanel, BorderLayout.EAST);
		
//		this.add(schedule, BorderLayout.CENTER);
		this.add(weatherLabel, BorderLayout.SOUTH);
		
		runwayPanel.setLayout(new GridLayout(4,2,10,10));
		for(int i = 0; i < 4; i++){
			runwayPanel.add(runway[i]);
			runwayPanel.add(runwayStatus[i]);
		}
		runwayPanel.setBorder(new LineBorder(Color.BLACK));
		this.add(runwayPanel,BorderLayout.CENTER);
		
		takeoff.setPreferredSize(new Dimension(100,495));
		landing.setPreferredSize(new Dimension(100,495));
		queuePanel.add(takeoff);
		queuePanel.add(landing);
		queuePanel.setBorder(new LineBorder(Color.BLACK));
		this.add(queuePanel,BorderLayout.WEST);
		
		this.setSize(600,600);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	public void addPlaneToTextArea(Plane plane, JTextArea textArea)
	{
		long time = plane.getEventTime();
		textArea.append("Plane " + Long.toString(time) + newline);
	}
	
	public void updateTakeoffQueueDisplays(Queue queue)
	{
		takeoff.setText("");
		for ( int i = 0; i < queue.getSize(); i++)
		{
			addPlaneToTextArea(queue.getObjectAtIndex(i),takeoff);
		}
	}
	public void updateLandingQueueDisplays(Queue queue)
	{
		landing.setText("");
		for ( int i = 0; i < queue.getSize(); i++)
		{
			addPlaneToTextArea(queue.getObjectAtIndex(i),landing);
		}
	}
	public void updateWeather(WeatherType type)
	{
		String string = "";
	
		switch(type){
		case RAIN:
			string = "Rain: 3 minutes per plane.";
			weatherLabel.setIcon(rainIcon);
			break;
		case WIND:
			string = "Wind: 2 runways closed, 8 minutes per plane.";
			weatherLabel.setIcon(windIcon);
			break;
		case SNOW:
			string = "Snow: 6 minutes per plane.";
			weatherLabel.setIcon(snowIcon);
			break;
		case SNOW_5:
			string = "Heavy Snow: 2 runways closed, 8 minutes per plane.";
			weatherLabel.setIcon(snowIcon);
			break;
		case SNOW_10:
			string = "Airport closed due to snow.";
			weatherLabel.setIcon(closedIcon);
			break;
		default:
			string  = "Sunny: 2 minutes per plane.";
			weatherLabel.setIcon(sunIcon);
		}
		weatherLabel.setText("Current Weather: " + string);
	}
	
	public void updateRunways(Runway[] runways)
	{
		for(int i = 0 ; i < runways.length; i++)
		{
			if(runways[i].getStatus())
			{
				runway[i].setText("Runway " + (i+1) + ":" + " Open");
				runwayStatus[i].setIcon(openIcon);
				
				Plane plane = runways[i].getPlaneOnRunway();
				
				if(plane != null){
					if(plane.getType() == simulation.Type.LANDING)
					{
						runway[i].setText("Runway " + (i+1) + ":" + " Plane Landing");
						runwayStatus[i].setIcon(landingIcon);
					}
					else
					{
						runway[i].setText("Runway " + (i+1) + ":" + " Plane Taking Off");
						runwayStatus[i].setIcon(takeoffIcon);
					}
				}
			}
			else
			{
				runway[i].setText("Runway " + (i+1) + ":" + " Closed");
				runwayStatus[i].setIcon(closedIcon);
			}
		}
	}
	
}
